package com.tansuo365.test1.controller.othercontrol;

import com.tansuo365.test1.bean.otherbean.Menu;
import com.tansuo365.test1.service.otherservice.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "权限菜单控制层",tags = "权限菜单控制接口 MenuController", description = "权限菜单控制层,作为权限菜单的操作.")
@RestController
@RequestMapping("/path")
public class MenuController {

    @Autowired
    private MenuService menuService;
    /**
     * 查询所有Menu
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询所有权限菜单", notes = "查询所有权限菜单listAllMenu")
    @RequestMapping("/listAllMenu")
//    @RequiresPermissions("路径配置")
    public Map<String, Object> listMenu() throws Exception {
        System.out.println("第一次走数据库");
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("rows", menuService.list());
        return resultMap;
    }

    /**
     * 动态查询所有Menu
     *
     * @param menu
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "动态查询所有权限菜单", notes = "动态查询所有权限菜单listAllMenuSelective")
    @RequestMapping("/listAllMenuSelective")
//    @RequiresPermissions("路径配置")
    public Map<String, Object> listMenu(Menu menu) throws Exception {
        System.out.println("in listAllMenuSelective");
        Map<String, Object> resultMap = new HashMap<>();
        List<Menu> menuList = menuService.listSelect(menu);
        resultMap.put("rows", menuList);
        resultMap.put("total", menuList.size());
        return resultMap;
    }

    /**
     * 删除Emenu元组
     *
     * @throws Exception
     */
    @ApiOperation(value = "删除权限菜单", notes = "删除权限菜单deletmenuById")
    @PostMapping("/delete")
//    @RequiresPermissions("路径配置")
    public Map<String, Object> delete(@RequestParam("ids") int[] ids) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        int deleteCode = menuService.deleteBatchByPKArr(ids);
        if (deleteCode == 1) {
            resultMap.put("success", true);
        } else {
            resultMap.put("success", false);
            resultMap.put("errorInfo", "删除失败了");
        }
        return resultMap;
    }

    /**
     * 添加菜单信息
     *
     * @param menu
     * @return
     */
    @ApiOperation(value = "保存(添加)权限菜单", notes = "保存权限菜单savmenu")
    @PostMapping("/save")
//    @RequiresPermissions("路径配置")
    public Map<String, Object> save(Menu menu) {
        //menu含有id录入,因为menu的id不是自动生成而是手动给与
        Map<String, Object> resultMap = new HashMap<>();
        int insertCode = menuService.add(menu);
        if (insertCode > 0) {
            resultMap.put("success", true);
            resultMap.put("saveMessage", "菜单添加成功!");
        } else {
            resultMap.put("success", false);
            resultMap.put("errorInfo", "菜单添加失败!");
        }
        return resultMap;
    }

    /**
     * 更新菜单信息
     *
     * @param menu
     * @return
     */
    @ApiOperation(value = "更新权限菜单", notes = "更新菜单信息updatmenu")
    @PostMapping("/update")
//    @RequiresPermissions("路径配置")
    public Map<String, Object> update(Menu menu) {
        //menu含有id录入,因为menu的id不是自动生成而是手动给与
        Map<String, Object> resultMap = new HashMap<>();
        int updateCode = menuService.update(menu);
        if (updateCode > 0) {
            resultMap.put("success", true);
            resultMap.put("saveMessage", "菜单更新成功!");
        } else {
            resultMap.put("success", false);
            resultMap.put("errorInfo", "菜单更新失败!");
        }
        return resultMap;
    }

}
